/**
 * Copyright 2008 floop.pl
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **/
package org.eaicompiler.sqlgen.builders;

import java.util.Iterator;
import java.util.SortedMap;

import org.eaicompiler.model.ScriptDefinition;

/**
 * Builder for Mangos SQL update
 * 
 * @author floop.pl
 */
public class MangosSQLBuilder extends SQLBuilder {
	private StringBuilder							sql;
	private boolean									sqlBuilt	= false;
	private SortedMap<Integer, ScriptDefinition>	creatures;

	public MangosSQLBuilder(boolean useDelete, SortedMap<Integer, ScriptDefinition> creatures) {
		super();
		this.sql = new StringBuilder();
		this.creatures = creatures;
	}

	public String buildSQLForCreatures() {
		if (!sqlBuilt) {
			boolean firstInsert = true;
			boolean firstElement = true;
			int count = 0;
			Iterator<Integer> it = creatures.keySet().iterator();
			while (it.hasNext()) {
				Integer id = it.next();
				if (firstInsert) {
					sql.append("update `creature_template` set scriptname = \"mob_eventai\" where entry in\n\t(");
					firstInsert = false;
				}
				if (!firstElement) {
					sql.append(",");
				} else {
					firstElement = false;
				}
				if (count % MAX_DELETE_IN_LINE == MAX_DELETE_IN_LINE-1) {
					sql.append("\n\t");
				}
				sql.append("'");
				sql.append(id.intValue());
				sql.append("'");
				count++;				
			}
			if(!firstElement)
			{
				sql.append(");\n");
			}			
			sqlBuilt = true;			
		}
		return sql.toString();
	}
}
